#include "npc.h"
#include <QTextCodec>
#include <QUrl>
#include <QDebug>

QTextCodec * gbkcodec = QTextCodec::codecForName("GBK");

npc::npc(const QNetworkProxy::ProxyType type, const QString Host, int Port, const QString Note, const QString Name, const QString Pwd):proxy(type, Host, Port, Name, Pwd){
    _note = Note;
    timer = 0;
    target = 0;
    clocker = 0;
    costTime = 0;
}

void npc::start(QString url, QString Key, int sec){
    key = Key;
    QUrl temp(url);
    if (timer == 0){
	timer = new QTimer();
	target = new QHttp();
	clocker = new QTime();
	connect (target, SIGNAL(done(bool)), this, SLOT(checkKey(bool)));
	connect (timer , SIGNAL(timeout()), this , SLOT(timeout()));
	target->setProxy(proxy);
    }
    target->setHost(temp.host(), temp.port(80));
    timer->start(sec*1000);
    clocker->restart();
    target->get(temp.path());
    //qDebug() << temp.path();
}

void npc::checkKey(bool error){
    costTime = clocker->elapsed();
    //qDebug() << "detect one response : " << costTime;
    QString query;
    timer->stop();
    if (!error){
	QByteArray response = target->readAll();
	if (-1 != response.indexOf("content=\"text/html; charset=gb2312\""))
	    query = gbkcodec->toUnicode(response);
	else
	    query = response;
	if (-1 == query.indexOf(key))
	    costTime = -1;
    }
    else{
	target->abort();
	costTime = -1;
    }
}

void npc::timeout(){
    timer->stop();
    target->abort();
    costTime = -1;
}


npc::~npc(){
    delete timer;
    delete target;
}

QString npc::ip() const {
    return tr("%1:%2").arg(proxy.hostName()).arg(proxy.port());
}

QString npc::time(int choose) const {
    if (costTime == 0 || choose == 0)
	return tr("Not checked");
    if (costTime == -1 || choose == -1)
	return tr("Time out");
    else
	return tr("%1 ms").arg(costTime);
}

QString npc::type() const{
    if (proxy.type() == QNetworkProxy::Socks5Proxy)
	return tr("SOCKS5");
    //if (proxy.type() == QNetworkProxy::HttpProxy)
    else
	return tr("HTTP");
}

QString npc::note() const{
    return tr("%1").arg(_note);
}

QString npc::save() const{
    return tr("%1@%2#%3").arg(ip()).arg(type()).arg(_note);
}

bool npc::operator<(const npc & other) const{
    return ip() < other.ip();
}

npcModel::npcModel(QObject * parent):QAbstractTableModel(parent){
    npcs = 0;
}

void npcModel::setSource(QList<npc *> * source){
    npcs = source;
}

int npcModel::rowCount (const QModelIndex & /*parent*/) const{
    //qDebug() << __FUNCTION__ << "()" ;
    if (npcs == 0 ) return 0;
    //qDebug() << npcs->count();
    return npcs->count();
}

int npcModel::columnCount (const QModelIndex & /*parent*/) const{
    return 4;
}
QVariant npcModel::data(const QModelIndex & index, int role) const{
    if (!index.isValid())
	return QVariant();
    if (role == Qt::TextAlignmentRole)
	return int (Qt::AlignLeft | Qt::AlignVCenter);
    else if (role == Qt::DisplayRole){
	npc * proxy = npcs->at(index.row());
	switch(index.column()){
	case 0:
	    return proxy->ip();
	case 1:
	    return proxy->type();
	case 2:
	    return proxy->time();
	case 3:
	default:
	    return proxy->note();
	}
    }
    return QVariant();
}

QVariant npcModel::headerData(int section, Qt::Orientation oritation, int role) const{
    if (role != Qt::DisplayRole)
	return QVariant();
    if (oritation == Qt::Vertical)
	return tr("No.%1").arg(section);
    if (section == 0)
	return tr("                         Proxy                         ");
    else if (section == 1)
	return tr("Type  ");
    else if (section == 2)
	return tr("Time  ");
    else if (section == 3)
	return tr("Comment  ");
    return QVariant();
}

void npcModel::fresh(){
    reset();
}
